<script setup>
import { ref, watch } from "vue";
import { User, Lock, Right, Back } from "@element-plus/icons-vue"
import { userRegisterService, userLoginService } from "@/api/user";
import { ElMessage } from "element-plus";
import { useUserStore } from '@/stores/index'
// import { useRoute } from "vue-router";
import { useRouter } from "vue-router";

const router = useRouter()
const userStore = useUserStore()

const isRegister = ref(false)
// 注册
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
})

const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: 'blur' },
    { min: 5, max: 10, message: "用户名必须是5-10位的字符", trigger: 'blur' }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur' },

  ],
  repassword: [
    { required: true, message: "请再次输入密码名", trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error("两次输入的密码不一致"))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const form = ref(null);
const register = async () => {
  await form.value.validate()
  try {
    await userRegisterService(formModel.value)
    ElMessage.success("注册成功")
    isRegister.value = false
    form.value.resetFields()
  } catch (error) {
    console.error('注册失败', error);
    return
  }



}
// 表单切换，重置表单
watch(isRegister, () => {
  form.value.resetFields()

})
const login = async () => {
  await form.value.validate()
  try {
    const res = await userLoginService(formModel.value)
    // res.data.token为null,则触发catch
    userStore.setToken(res.data.token)
    ElMessage.success('登录成功')
    router.push("/")
  } catch (error) {
    console.log(error);

    ElMessage.error("用户名或密码错误")
  }
}
</script>

<template>
  <!-- <div>登录页</div> -->
  <el-row>
    <el-col :span="12" :offset="6">
      <!-- 注册表单 -->
      <el-form autocomplete="off" size="large" v-if="isRegister" :model="formModel" :rules="rules" ref="form">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formModel.username">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon='Lock' placeholder="请输入密码" v-model="formModel.password">
          </el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input :prefix-icon='Lock' placeholder="请再次输入密码" v-model="formModel.repassword">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" @click="register">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-icon>
            <Back />
          </el-icon>
          <el-text class="register" @click="isRegister = false">返回登录</el-text>
        </el-form-item>
      </el-form>
      <!-- 登录表单 -->
      <el-form autocomplete="off" size="large" ref="form" :model="formModel" :rules="rules" v-else>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formModel.username">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon='Lock' placeholder="请输入密码" v-model="formModel.password">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox-group>
            <el-checkbox label="记住我" />
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" @click="login">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-text class="register" @click="isRegister = true">注册</el-text>
          <el-icon>
            <Right />
          </el-icon>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
button.el-button.el-button--primary.el-button--large {
  width: 100%;
}

.register {
  cursor: pointer;
}
</style>
